/**
 * 
 */
package com.antilia.infomaresme.data.dataobjects;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * @author Ernesto
 *
 */
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames={"categoryPath","user_fk", "geographicPath"})})
public class CategoryAlert extends DomainObject {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name="user_fk")
	private User user;
	
	/**
	 * String identifying the category.
	 */
	private String title;
	
	/**
	 * String representing a (business) category path for the item.
	 * 
	 * (e.g. >>1>>12>34>>)
	 */
	@org.hibernate.annotations.Index(name = "categoryPathIndex")
	private String categoryPath;
	
	private String categoryTitle;
	
	/**
	 * String representing a (geographic) category path for the item.
	 * 
	 * (e.g. >>1>>12>34>>)
	 */
	@org.hibernate.annotations.Index(name = "geographicPathIndex")
	private String geographicPath;	
	
	private String geographicLocation;
	
	private Frequency emailFrequency;
	
	private String events;
	
	
	/**
	 * Constructor.
	 */
	public CategoryAlert() {
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCategoryPath() {
		return categoryPath;
	}

	public void setCategoryPath(String categoryPath) {
		this.categoryPath = categoryPath;
	}

	public void setCategoryPath(Category category) {
		this.categoryPath = Item.buildCategory(category);
		this.categoryTitle = Item.buildTitle(category);
	}
	
	public void setGeographicPath(Category category) {
		this.geographicPath = Item.buildCategory(category);
		this.geographicLocation = Item.buildTitle(category);
	}
	
	public String getGeographicPath() {
		return geographicPath;
	}

	public void setGeographicPath(String geographicPath) {
		this.geographicPath = geographicPath;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Frequency getEmailFrequency() {
		return emailFrequency;
	}

	public void setEmailFrequency(Frequency emailFrequency) {
		this.emailFrequency = emailFrequency;
	}

	public String getCategoryTitle() {
		return categoryTitle;
	}

	public void setCategoryTitle(String categoryTitle) {
		this.categoryTitle = categoryTitle;
	}
	
	public String getEvents() {
		return events;
	}

	public void setEvents(String events) {
		this.events = events;
	}
	
	public void setEvents(List<CategoryEventSubject> events) {
		if(events == null || events.size() == 0) {			
			this.events = null;
			return;
		}
		StringBuilder sb  = new StringBuilder();
		for(CategoryEventSubject event: events) {
			sb.append(event.name());
			sb.append(",");
		}
		this.events = sb.toString();
	}
	
	public List<CategoryEventSubject> getEventsAsEnum() {
		if(events != null) {
			List<CategoryEventSubject> eventSubjects = new ArrayList<CategoryEventSubject>();
			String[] sev = events.split(",");
			for(String  string: sev) {
				try {
					eventSubjects.add(CategoryEventSubject.valueOf(string));
				} catch (Exception e) {
					
				}
			}
			return eventSubjects;
		}
		return null;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getGeographicLocation() {
		return geographicLocation;
	}

	public void setGeographicLocation(String geographicLocation) {
		this.geographicLocation = geographicLocation;
	}

}
